﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Mangox.Common;
using Mgx.Job.Jobs;
using Quartz;

namespace Mgx.Jobs
{
    /// <summary>
    /// API保持在线
    /// </summary>
    public class KeepOnline : BaseJob, IJob
    {
        protected static string className = (new StackTrace()).GetFrame(1).GetMethod().ReflectedType.Name + "/";

        public void Execute(IJobExecutionContext context)
        {
            base.log.Info("任务 KeepOnline 开始");
            Stopwatch watch = new Stopwatch();
            watch.Start();

            try
            {
                Run("https://www.jsonfree.com/api/stock/test");
                Run("https://www.jsonfree.com/mgxapi/api/Mangox/test");
                Run("https://www.jsonfree.com/mgxapi.test/api/Mangox/test");
            }
            catch (Exception ex)
            {
                base.log.Info("任务 KeepOnline 异常:" + ex);
            }

            watch.Stop();
            base.log.Info("任务 KeepOnline 结束,任务总耗时：" + watch.ElapsedMilliseconds / 1000 + "秒");
        }

        private void Run(string url)
        {
            JsonRequest request = new JsonRequest();
            string result = null;
            try
            {
                result = request.Get(url);
                Console.WriteLine($"result-{result}");
                log.InfoFormat(className + $"Run result-{result}");
            }
            catch (Exception ex)
            {
                log.ErrorFormat(className + $"Run result-{result}，Fail-{ex.ToString()}");
            }
        }

    }
}
